//获取当前定位

// #ifdef APP-PLUS
import permision from "@/common/utils/permission.js";
// #endif
// #ifndef APP-PLUS
import amap from "@/common/libs/gaode/amap-wx.js";
// #endif
import QQMapWX from '@/common/libs/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js'
export async function getLocation() {
	//首先检查权限
	// #ifdef APP-PLUS
	//安卓检查权限有问题，暂时先不检查权限了
	// let status = await checkPermission();
	// console.log('status', status)
	// if (status !== 1) {
	//     return;
	// }
	// #endif
	// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ || MP-ALIPAY
	let status = await getSetting();
	if (status === 2) {
		uni.showModal({
			content: "请求访问您的位置信息",
			confirmText: "设置",
			success: function(res) {
				if (res.confirm) {
					openSetting();
				}
			},
		});
		return;
	}
	// #endif

	//然后发起定位
	return new Promise((resolve, reject) => {
		console.log("发起定位");
		// #ifdef APP-PLUS
		console.log("app端截取成功");
		uni.getLocation({
			type: "gcj02", //gcj02 wgs84
			geocode: true,
			success: (res) => {
				console.log("APP定位成功", res);
				var address = res.address
				address.latitude = res.latitude
				address.longitude = res.longitude
				resolve({
					code: 200,
					data: address
				});
			},
			fail: (err) => {
				console.log("APP定位失败", err);
				reject(err);
			},
		});
		// #endif
		// #ifndef APP-PLUS
		//初始化腾讯SDK对象
		var qqmapsdk = new QQMapWX({
			key: "A5SBZ-TPK6W-M5VRM-YQT7K-LACYQ-5VBMB",
		});
		uni.getLocation({
			type: "gcj02", //gcj02 wgs84
			geocode: true,
			altitude: true,
			isHighAccuracy: true,
			success: (res) => {
				console.log("小程序定位成功", res);
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude
					},
					success: function(res) {
						console.log('解析成功', res);
						resolve({
							code: '200',
							data: res.result
						});
					},
					fail: function(res) {
						console.log('解析失败', res);
						resolve({
							code: '500',
							data: {},
							messee: '解析失败'
						});
					},
				});


			},
			fail: (err) => {
				console.log("小程序定位失败", err);
				resolve({
					code: '500',
					data: {},
					messee: '定位失败'
				});
			},
		});


		// #endif
	});
}

function getSetting() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (res) => {
				if (res.authSetting["scope.userLocation"] === undefined) {
					resolve(0);
					return;
				}
				if (res.authSetting["scope.userLocation"]) {
					resolve(1);
				} else {
					resolve(2);
				}
			},
		});
	});
}

function openSetting() {
	// this.hideConfirm();
	uni.openSetting({
		success: (res) => {
			if (res.authSetting && res.authSetting["scope.userLocation"]) {}
		},
		fail: (err) => {},
	});
}
async function checkPermission() {
	console.log("开始检查权限");
	let status = permision.isIOS ? await permision.requestIOS("location") : 1; //ios才做权限判断，安卓有问题，不在这里做权限判断
	console.log("权限status", status);
	if (status === null || status === 1) {
		status = 1;
	} else if (status === 2) {
		uni.showModal({
			content: "系统定位已关闭",
			confirmText: "确定",
			showCancel: false,
			success: function(res) {},
		});
	} else if (status.code) {
		uni.showModal({
			content: status.message,
		});
	} else {
		uni.showModal({
			content: "需要定位权限",
			confirmText: "设置",
			success: function(res) {
				if (res.confirm) {
					permision.gotoAppSetting();
				}
			},
		});
	}

	return status;
}
